package com.murong.ai.coder.infrastructure.config;

import com.murong.ai.coder.entity.GlobalProperties;
import jakarta.annotation.PostConstruct;
import jakarta.xml.bind.JAXBContext;
import jakarta.xml.bind.JAXBException;
import jakarta.xml.bind.Unmarshaller;
import lombok.SneakyThrows;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.io.File;

@Configuration
public class GlobalConfiguration {
    private static GlobalProperties globalProperties;

    @PostConstruct
    public void init() {
        globalProperties = loadProperties();
    }

    @Bean
    public GlobalProperties globalProperties() {
        return globalProperties;
    }

    private GlobalProperties loadProperties() {
        try {
            JAXBContext context = JAXBContext.newInstance(GlobalProperties.class);
            Unmarshaller unmarshaller = context.createUnmarshaller();
            File file = new File("/Users/haoyulin/IdeaProjects/cbs_dev/customer/.prompt-word/global-configuration.xml");
            return (GlobalProperties) unmarshaller.unmarshal(file);
        } catch (JAXBException e) {
            throw new RuntimeException("解析global-configuration.xml失败", e);
        }
    }

//    @SneakyThrows
//    @Bean
//    public freemarker.template.Configuration configuration() {
//        freemarker.template.Configuration cfg = new freemarker.template.Configuration(freemarker.template.Configuration.VERSION_2_3_32);
//        cfg.setDirectoryForTemplateLoading(new File("src/main/resources/fmt"));
//        cfg.setDefaultEncoding("UTF-8");
//        return cfg;
//    }

}